package com.pcitc.paas.feign.demo.api.processor;

import com.pcitc.paas.feign.demo.api.annotation.RequestObject;
import feign.MethodMetadata;
import feign.Util;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.util.ClassUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * description: <br>
 * createTime: 2021/6/21 17:47 <br>
 *
 * @author zzh
 */
public class RequestObjectParameterProcessor implements AnnotatedParameterProcessor {
    private static final Class<RequestObject> ANNOTATION = RequestObject.class;

    public RequestObjectParameterProcessor() {
    }

    public Class<? extends Annotation> getAnnotationType() {
        return ANNOTATION;
    }

    public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {
        int parameterIndex = context.getParameterIndex();
        Class<?> parameterType = method.getParameterTypes()[parameterIndex];
        MethodMetadata data = context.getMethodMetadata();
        if (!ClassUtils.isPrimitiveOrWrapper(parameterType)) {
            Util.checkState(data.queryMapIndex() == null, "Query map can only be present once.", new Object[0]);
            data.queryMapIndex(parameterIndex);
            return true;
        } else {
            return true;
        }
    }
}
